package com.atguigu.fruit.dao.impl;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.basedao.BaseDAO;
import org.junit.Test;

import java.util.List;

public class FruitDAOImpl extends BaseDAO<Fruit> implements FruitDAO {
    @Override


    public List<Fruit> getFruitList(String keyword,Integer pageNo) {

        return super.executeQuery("select * from fruit where fname like ? or remark like ? limit ?,5","%"+keyword+"%","%"+keyword+"%",(pageNo-1)*5);
    }



    @Test
    public  void  getFruitList1() {
        System.out.println(super.executeQuery("select * from fruit"));
    }

    public Fruit getFruitbyFid(int fid){
        String sql = "select * from fruit where fid=?";
        return super.load(sql,fid);
    }

    public void update(Fruit fruit){
            String sql = "update fruit set fname = ? , price = ? , fcount = ? , remark = ? where fid = ?";
            super.executeUpdate(sql,fruit.getFname(),fruit.getPrice(),fruit.getFcount(),fruit.getRemark(),fruit.getFid());
    }

    public void addFruit(Fruit fruit){
        String sql = "insert into fruit values(0,?,?,?,?)";
        super.executeUpdate(sql,fruit.getFname(),fruit.getPrice(),fruit.getFcount(),fruit.getRemark());

    }

    public void delFruit(Integer fid){

            super.executeUpdate("delete from fruit where fid=?",fid);
    }

    public int countFruit(String keyword){

       return ((Long)super.executeComplexQuery("select count(*) from fruit where fname like ? or remark like ?","%"+keyword+"%","%"+keyword+"%")[0]).intValue();
    }

    public List<Fruit> getFruitSearch(String keyword){
        String sql = "select * from fruit where fname like ? and remark like ?";
        return super.executeQuery(sql,"%"+keyword+"%","%"+keyword+"%");
    }
}
